﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _3TierDemo.BIZ;
using _3TierDemo.DTO;
using _3TierDemo.Common;
namespace _3TierDemo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            loadDataForGridViewLop();
        }

        public void loadDataForGridViewLop()
        {
            LopBIZ biz = new LopBIZ();
            dgvSV.DataSource = biz.getAll();

        }

        #region Methods

  
        public void doInsert()
        {
            LopDTO obj = loadDataFromControlsToDTO();
            LopBIZ biz = new LopBIZ();
            biz.insert(obj);
        }

        public void doUpdate()
        {
            LopDTO obj = new LopDTO();
            obj.Ma = Convert.ToInt32(txtMa.Text);
            obj.Ten = txtTen.Text;
            obj.TenGV = txttengv.Text;

            LopBIZ biz = new LopBIZ();
            biz.update(obj);
        }

        public void doDelete()
        {
            int id = Convert.ToInt32(txtMa.Text);
            LopBIZ biz = new LopBIZ();
            biz.deleteByID(id);
        }

        public void doClear()
        {
            txtMa.Text = string.Empty;
            txtTen.Text = string.Empty;
            txttengv.Text = "";
            txtMa.ReadOnly = false;
        }

        private LopDTO loadDataFromControlsToDTO()
        {
            LopDTO obj = new LopDTO();
            obj.Ma = Convert.ToInt32(txtMa.Text);
            obj.Ten = txtTen.Text;
            obj.TenGV =txttengv.Text;
            return obj;
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            doInsert();
            loadDataForGridViewLop();
            doClear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            doUpdate();
            loadDataForGridViewLop();
            doClear();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            doDelete();
            loadDataForGridViewLop();
            doClear();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            doClear();
        }

        private void dgvSV_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvSV.SelectedRows.Count > 0)
            {
                int ma = Convert.ToInt32(dgvSV.SelectedRows[0].Cells[0].Value);
                LopBIZ biz = new LopBIZ();
                LopDTO obj = biz.getByID(ma);
                txtMa.Text = obj.Ma.ToString();
                txtTen.Text = obj.Ten;
                txttengv.Text = obj.TenGV;
                
            }
        }
    }
}
